package org.martin.snsrs.client;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnClickListener,
		OnCheckedChangeListener {

	private Intent categroyIntent;
	private Intent describeIntent;
	private Intent hotEventIntent;
	private ImageView indication1;
	private ImageView indication2;
	private ImageView indication3;
	private ImageButton publishNewActivity;
	private ImageButton refreshActivity;
	private RadioButton[] radioButton;
	private RadioGroup radioGroup;
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		tabHost = getTabHost();
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				
				
			}
		});
		setupIntent();
		resetTabs(0);
	}

	private void init() {
		if (this.hotEventIntent == null)
			this.hotEventIntent = new Intent(this, HotEventActivity.class);
		if (this.categroyIntent == null)
			this.categroyIntent = new Intent(this, EventListActivity.class);
		if (this.describeIntent == null)
			this.describeIntent = new Intent(this, RecActivity.class);
		this.indication1 = ((ImageView) findViewById(R.id.imagetab1));
		this.indication2 = ((ImageView) findViewById(R.id.imagetab2));
		this.indication3 = ((ImageView) findViewById(R.id.imagetab3));
		this.radioGroup = ((RadioGroup) findViewById(R.id.rg));
		try{
		this.publishNewActivity = (ImageButton) findViewById(R.id.edit_new_activity);
		publishNewActivity.setOnClickListener(this);
		this.refreshActivity = (ImageButton) findViewById(R.id.refresh);
		refreshActivity.setOnClickListener(this);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		this.radioButton = new RadioButton[3];
		for (int i = 0; i < 3; i++) {
			this.radioButton[i] = ((RadioButton) this.radioGroup
					.findViewWithTag("radio_button" + i));
			this.radioButton[i].setOnCheckedChangeListener(this);
			this.radioButton[i].setOnClickListener(this);
		}
		this.radioButton[0].setTextColor(-1);
		this.radioButton[0].setTextSize(12.0F);
		this.radioButton[1].setTextColor(-1);
		this.radioButton[1].setTextSize(12.0F);
		this.radioButton[2].setTextColor(-1);
		this.radioButton[2].setTextSize(12.0F);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked && buttonView.equals(radioButton[0])) {
			resetTabs(0);
			this.tabHost.setCurrentTab(0);
		}
		if (isChecked && buttonView.equals(radioButton[1])) {
			resetTabs(1);
			this.tabHost.setCurrentTab(1);
		}
		if (isChecked && buttonView.equals(radioButton[2])) {
			resetTabs(2);
			this.tabHost.setCurrentTab(2);
		}
	}

	@Override
	public void onClick(View v) {
		if(v == publishNewActivity){
			Intent intent = new Intent();
			intent.setClass(this, EditNewActivity.class);
			startActivity(intent);
			this.finish();
			return;
		}
		for (int i = 0; i < 3; i++) {
			if (v == radioButton[i]) {
				switchTab(i);
			}
		}
	}

	public void resetTabs(int paramInt) {
		this.indication1.setVisibility(RadioButton.INVISIBLE);
		this.indication2.setVisibility(RadioButton.INVISIBLE);
		this.indication3.setVisibility(RadioButton.INVISIBLE);
		this.radioButton[0]
				.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
						.getDrawable(R.drawable.icon_tab_hot), null, null);
		this.radioButton[0].setTextColor(-5723992);
		this.radioButton[1].setCompoundDrawablesWithIntrinsicBounds(null,
				getResources().getDrawable(R.drawable.icon_tab_category), null,
				null);
		this.radioButton[1].setTextColor(-5723992);
		this.radioButton[2].setCompoundDrawablesWithIntrinsicBounds(null,
				getResources().getDrawable(R.drawable.icon_tab_my), null, null);
		this.radioButton[2].setTextColor(-5723992);
		this.radioButton[paramInt].setTextColor(-2269126);
		if (paramInt == 0) {
			this.indication1.setVisibility(0);
			this.radioButton[0].setCompoundDrawablesWithIntrinsicBounds(
					null,
					getResources().getDrawable(
							R.drawable.icon_tab_hot_activated), null, null);
		}
		if (paramInt == 1) {
			this.indication2.setVisibility(0);
			this.radioButton[1]
					.setCompoundDrawablesWithIntrinsicBounds(
							null,
							getResources().getDrawable(
									R.drawable.icon_tab_category_activated),
							null, null);
		}
		if (paramInt == 2) {
			this.indication3.setVisibility(0);
			this.radioButton[2].setCompoundDrawablesWithIntrinsicBounds(null,
					getResources()
							.getDrawable(R.drawable.icon_tab_my_activated),
					null, null);
		}
	}

	public void switchTab(int i) {
		switch (i) {
		case 0:
			this.radioButton[0].toggle();
			this.tabHost.setCurrentTab(0);
			return;
		case 1:
			this.radioButton[1].toggle();
			this.tabHost.setCurrentTab(1);
			return;
		case 2:
			this.radioButton[2].toggle();
			this.tabHost.setCurrentTab(2);
		default:
			return;
		}

	}

	public void setupIntent() {
		try {
			this.tabHost.addTab(this.tabHost
					.newTabSpec("mblog_tab")
					.setIndicator(getString(R.string.tab_host_label),
							getResources().getDrawable(R.drawable.icon_tab_hot))
					.setContent(this.hotEventIntent));
			this.tabHost.addTab(this.tabHost
					.newTabSpec("message_tab")
					.setIndicator(getString(R.string.tab_categroy_label),
							getResources().getDrawable(R.drawable.icon_tab_category))
					.setContent(this.categroyIntent));
			this.tabHost.addTab(this.tabHost
					.newTabSpec("userinfo_tab")
					.setIndicator(getString(R.string.tab_describe_label),
							getResources().getDrawable(R.drawable.icon_tab_my))
					.setContent(this.describeIntent));
			return;
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}
}
